deploy/main: Unify some bits between admin-switch and admin-upgrade
authorColin Walters <walters@verbum.org>
Sun, 19 Jan 2014 16:28:30 +0000 (11:28 -0500)
committerColin Walters <walters@verbum.org>
Sun, 19 Jan 2014 16:28:30 +0000 (11:28 -0500)
src/ostree/ot-admin-builtin-switch.c
src/ostree/ot-admin-builtin-upgrade.c
src/ostree/ot-admin-functions.c
src/ostree/ot-admin-functions.h

index e524de851fdb4dce783baa7a715bd1614de82cfd..d74461846096aaac06649d6eea181526d4f96227 100644 (file)
@@ -73,38 +73,13 @@ ot_admin_builtin_switch (int argc, char **argv, OstreeSysroot *sysroot, GCancell
   if (!ostree_sysroot_load (sysroot, cancellable, error))
     goto out;
 
-  if (!ot_admin_require_booted_deployment_or_osname (sysroot, opt_osname,
-                                                     cancellable, error))
-    goto out;
-  merge_deployment = ostree_sysroot_get_merge_deployment (sysroot, opt_osname); 
-  if (merge_deployment == NULL)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No previous deployment for OS '%s'", opt_osname);
-      goto out;
-    }
-
-  deployment_path = ostree_sysroot_get_deployment_directory (sysroot, merge_deployment);
-  deployment_origin_path = ostree_sysroot_get_deployment_origin_path (deployment_path);
-
   if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error))
     goto out;
 
-  origin = ostree_deployment_get_origin (merge_deployment);
-  if (!origin)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No origin known for current deployment");
-      goto out;
-    }
-  origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL);
-  if (!origin_refspec)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No origin/refspec in current deployment origin; cannot change via ostree");
-      goto out;
-    }
-  if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error))
+  if (!ot_admin_deploy_prepare (sysroot, opt_osname, &merge_deployment,
+                                &origin_remote, &origin_ref,
+                                &origin,
+                                cancellable, error))
     goto out;
 
   if (strcmp (origin_ref, new_ref) == 0)
@@ -114,6 +89,16 @@ ot_admin_builtin_switch (int argc, char **argv, OstreeSysroot *sysroot, GCancell
       goto out;
     }
 
+  {
+    gs_free char *new_refspec = NULL;
+    if (origin_remote)
+      new_refspec = g_strconcat (origin_remote, ":", new_ref, NULL);
+    else
+      new_refspec = g_strdup (new_ref);
+    g_key_file_unref (origin);
+    origin = ostree_sysroot_origin_new_from_refspec (sysroot, new_refspec);
+  }
+
   if (origin_remote)
     {
       OstreeRepoPullFlags pullflags = 0;
index d222e9e0a8e9c7315b65499a4b46659768bd2932..1b86cddcc10038f3ebd58f986e487b84cf0a57ff 100644 (file)
@@ -67,38 +67,14 @@ ot_admin_builtin_upgrade (int argc, char **argv, OstreeSysroot *sysroot, GCancel
   if (!ostree_sysroot_load (sysroot, cancellable, error))
     goto out;
 
-  if (!ot_admin_require_booted_deployment_or_osname (sysroot, opt_osname,
-                                                     cancellable, error))
-    goto out;
-  merge_deployment = ostree_sysroot_get_merge_deployment (sysroot, opt_osname); 
-  if (merge_deployment == NULL)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No previous deployment for OS '%s'", opt_osname);
-      goto out;
-    }
-
-  deployment_path = ostree_sysroot_get_deployment_directory (sysroot, merge_deployment);
-  deployment_origin_path = ostree_sysroot_get_deployment_origin_path (deployment_path);
 
   if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error))
     goto out;
 
-  origin = ostree_deployment_get_origin (merge_deployment);
-  if (!origin)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No origin known for current deployment");
-      goto out;
-    }
-  origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL);
-  if (!origin_refspec)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                   "No origin/refspec in current deployment origin; cannot upgrade via ostree");
-      goto out;
-    }
-  if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error))
+  if (!ot_admin_deploy_prepare (sysroot, opt_osname, &merge_deployment,
+                                &origin_remote, &origin_ref,
+                                &origin,
+                                cancellable, error))
     goto out;
 
   if (origin_remote)
index 6d81655d1384c3fc88f07064dba2d35986b2ecb2..6b54fb2c168524967133dc648387531796559d6c 100644 (file)
@@ -106,3 +106,63 @@ ot_admin_complete_deploy_one (OstreeSysroot      *sysroot,
  out:
   return ret;
 }
+
+gboolean
+ot_admin_deploy_prepare (OstreeSysroot      *sysroot,
+                         const char         *osname,
+                         OstreeDeployment  **out_merge_deployment,
+                         char              **out_origin_remote,
+                         char              **out_origin_ref,
+                         GKeyFile          **out_origin,
+                         GCancellable        *cancellable,
+                         GError             **error)
+{
+  gboolean ret = FALSE;
+  gs_free char *origin_refspec = NULL;
+  gs_free char *origin_remote = NULL;
+  gs_free char *origin_ref = NULL;
+  gs_unref_object GFile *deployment_path = NULL;
+  gs_unref_object GFile *deployment_origin_path = NULL;
+  gs_unref_object OstreeDeployment *merge_deployment = NULL;
+  GKeyFile *origin;
+
+  if (!ot_admin_require_booted_deployment_or_osname (sysroot, osname,
+                                                     cancellable, error))
+    goto out;
+  merge_deployment = ostree_sysroot_get_merge_deployment (sysroot, osname); 
+  if (merge_deployment == NULL)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "No previous deployment for OS '%s'", osname);
+      goto out;
+    }
+
+  deployment_path = ostree_sysroot_get_deployment_directory (sysroot, merge_deployment);
+  deployment_origin_path = ostree_sysroot_get_deployment_origin_path (deployment_path);
+
+  origin = ostree_deployment_get_origin (merge_deployment);
+  if (!origin)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "No origin known for current deployment");
+      goto out;
+    }
+  origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL);
+  if (!origin_refspec)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                   "No origin/refspec in current deployment origin; cannot upgrade via ostree");
+      goto out;
+    }
+  if (!ostree_parse_refspec (origin_refspec, &origin_remote, &origin_ref, error))
+    goto out;
+
+  ret = TRUE;
+  gs_transfer_out_value (out_merge_deployment, &merge_deployment);
+  gs_transfer_out_value (out_origin_remote, &origin_remote);
+  gs_transfer_out_value (out_origin_ref, &origin_ref);
+  gs_transfer_out_value (out_origin, &origin);
+ out:
+  g_clear_pointer (&origin, g_key_file_unref);
+  return ret;
+}
index 1b26948d0de9612ee782952891a8bd5cc3d595c2..d42c974cb70e0857a803327df033803b5112cf82 100644 (file)
@@ -33,6 +33,16 @@ ot_admin_require_booted_deployment_or_osname (OstreeSysroot       *sysroot,
                                               GCancellable        *cancellable,
                                               GError             **error);
 
+gboolean
+ot_admin_deploy_prepare (OstreeSysroot      *sysroot,
+                         const char         *osname,
+                         OstreeDeployment  **merge_deployment,
+                         char              **origin_remote,
+                         char              **origin_ref,
+                         GKeyFile          **out_origin,
+                         GCancellable        *cancellable,
+                         GError             **error);
+
 gboolean
 ot_admin_complete_deploy_one (OstreeSysroot      *sysroot,
                               const char         *osname,